import sys #控制游戏的退出

import pygame #完成游戏开发的功能
from pygame.sprite import Group
from settings import Settings
from ship import Ship
from alien import Alien
from game_stats import GameStats
from Scoreboard import Scoreboard
from button import Button

import game_functions as gf

def run_game():
    #初始化游戏并创建一个屏幕对象
    pygame.init()
    ai_settings = Settings()
    #screen是一个surface，surface是屏幕的一部分，每个元素都是一个surface,
    #set_mode()返回一个surface对象，其中包含一个显示窗口
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height)) 
    pygame.display.set_caption("Alien Invasion")

    #设置背景色
    bg_color = (230, 230, 230)
    
    #创建Play按钮
    play_button = Button(ai_settings, screen, 'Play')

    #创建一艘飞船
    ship = Ship(ai_settings ,screen)
    #创建一个外星人
    alien = Alien(ai_settings, screen)

    #创建一个用于存储子弹的编组和外星人编组
    bullets = Group()
    aliens = Group()

    #创建外星人群
    gf.create_fleet(ai_settings, screen, ship, aliens) 
         
    #创建一个用于存储游戏统计信息的实例，并创建积分牌
    stats = GameStats(ai_settings)
    sb = Scoreboard(ai_settings, screen, stats)

    #开始游戏主循环
    while True:

        #监视鼠标和键盘事件
        gf.check_events(ai_settings, screen, ship, bullets, stats, play_button, aliens, sb)
         
        if stats.game_active:
            ship.update()
            #删除已消失的子弹
            gf.update_bullets(ai_settings, screen, ship, aliens, bullets, stats, sb)
             #更新外星人位置
            gf.update_aliens(ai_settings,stats, screen, ship, aliens, bullets, sb)
            
        bullets.update()

        
       

        #更新屏幕
        gf.update_screen(ai_settings, screen, ship, aliens, bullets, play_button, stats, sb)

        
         
        
       

run_game()
